<?php

namespace app\admin\model;

use think\Db;
use think\Model;


class Vote extends Model
{
    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'vote';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'typedata_text',
        'starttime_text',
        'endtime_text',
        'status_text'
    ];

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changedata = $row->getChangedData();
            if (isset($changedata['user_id'])) {
                $userInfo = (new \app\common\model\User())->find($changedata['user_id']);
                $group_name = Db::name('user_group')->where('id',$userInfo->group_id)->value('name');
                $row->resource = $group_name;
            }
        });

        self::beforeInsert(function ($row) {
            $addData = $row->getData();
            if (isset($addData['user_id'])) {
                $userInfo = (new \app\common\model\User())->find($addData['user_id']);
                $group_name = Db::name('user_group')->where('id',$userInfo->group_id)->value('name');
                $row->resource = $group_name;
            }else{
                $row->resource = '站长';
            }
        });
    }

    public function getTypedataList()
    {
        return ['1' => __('Typedata 1'), '2' => __('Typedata 2'), '3' => __('Typedata 3')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getTypedataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['typedata']) ? $data['typedata'] : '');
        $list = $this->getTypedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function site()
    {
        return $this->belongsTo('User', 'site_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
